﻿@page "/app/settings/general"
@layout SettingsLayout
@inject SettingsVM SettingsVM
@inject IJSRuntime JSRuntime;
@using RatScanner.TarkovDev.GraphQL;
@implements IDisposable
<div>
    <MudGrid Class="pa-0 ma-0" Spacing="0" Style="height: 100%;">
        <MudItem xs="12">
            <div class="d-flex align-center justify-center mx-2 mb-1">
                <MudText Typo="Typo.subtitle1" Color="Color.Inherit">General Settings</MudText>
            </div>
        </MudItem>
        <MudItem xs="12">
            <MudPaper Class="align-center justify-center pa-2 mx-2 mb-2" Elevation="2">
                <MudSlider @bind-Value="SettingsVM.ToolTipMilli" Min="0" Max="10000" Step="100" Color="Color.Info">
                    <MudText Typo="Typo.subtitle2">Tooltip Duration: @SettingsVM.ToolTipMilli.ToString()</MudText>
                </MudSlider>
            </MudPaper>
        </MudItem>
        <MudItem xs="12">
            <MudPaper Class="align-center justify-center pa-2 mx-2 mb-2" Elevation="2">
                <MudText Style="margin-bottom: 4px">Screen Resolution</MudText>
                <MudGrid Spacing="1">
                    <MudItem xs="4">
                        <MudNumericField @bind-Value="SettingsVM.ScreenWidth" HideSpinButtons="true" Label="Width" Variant="Variant.Outlined" Margin="Margin.Dense" Min="100" Max="99999"></MudNumericField>
                    </MudItem>
                    <MudItem xs="4">
                        <MudNumericField @bind-Value="SettingsVM.ScreenHeight" HideSpinButtons="true" Label="Height" Variant="Variant.Outlined" Margin="Margin.Dense" Min="100" Max="99999"></MudNumericField>
                    </MudItem>
                    <MudItem xs="4">
                        <MudNumericField @bind-Value="SettingsVM.ScreenScale" Label="Scale" Variant="Variant.Outlined" HideSpinButtons="true" Converter="@percentageConverter" Margin="Margin.Dense" Min="0.1f" Max="8"></MudNumericField>
                    </MudItem>
                </MudGrid>
            </MudPaper>
        </MudItem>
        <MudItem xs="12">
            <MudPaper Class="align-center justify-center pa-2 mx-2 mb-2" Elevation="2">
                <MudGrid Spacing="0" Class="pa-0 ma-0">
                    <MudCheckBox @bind-Checked="@SettingsVM.GameMode" Label="Use PVE data" Dense="true" Size="Size.Small" Class="pa-0 ma-0" Converter="@gameModeConverter"></MudCheckBox>
                    <!--<MudCheckBox @bind-Checked="@SettingsVM.MinimizeToTray" Label="Minimize to tray" Dense="true" Size="Size.Small" Class="pa-0 ma-0"></MudCheckBox>-->
                    <MudCheckBox @bind-Checked="@SettingsVM.AlwaysOnTop" Label="Always on top" Dense="true" Size="Size.Small" Class="pa-0 ma-0"></MudCheckBox>
                    <MudCheckBox @bind-Checked="@SettingsVM.LogDebug" Label="Log debug info" Dense="true" Size="Size.Small" Class="pa-0 ma-0"></MudCheckBox>
                </MudGrid>
            </MudPaper>
        </MudItem>
    </MudGrid>
</div>

@code {

    MudBlazor.Converter<GameMode, bool?> gameModeConverter = new MudBlazor.Converter<GameMode, bool?> {
            SetFunc = value => value == GameMode.Pve,
            GetFunc = value => value ?? false ? GameMode.Pve : GameMode.Regular,
        };

    Converter<float> percentageConverter = new Converter<float> {
            SetFunc = value => (int)(value * 100) + "%",
            GetFunc = text => {
                text = text?.TrimEnd('%') ?? "";
                if (int.TryParse(text, out var num)) return num / 100f;
                return 1;
            },
        };

    protected override void OnInitialized() {
        SettingsVM.PropertyChanged += PropertyChangeHandler;
    }

    private async void PropertyChangeHandler(object? sender, EventArgs e) {
        await InvokeAsync(() => StateHasChanged());
    }

    public void Dispose() {
        SettingsVM.PropertyChanged -= PropertyChangeHandler;
    }

}
